import {
  C13BottomDock,
  Layout,
  PriceView,
  Title,
  UserSubPageBase,
} from "@/components";
import { redirectTo, useRouter } from "@tarojs/taro";
import useOrderStatusData from "@/hooks/useOrderStatusData";
import { switchTab } from "@/utils/navigation";
import { View } from "@tarojs/components";
import { useEffect, useState } from "react";
import { ApiItemTypeMapping } from "@/data/shared/entityShapes";
import { requestSubscribeMessage } from "@/store/userDataContext";
import notifications from "@/data/notifications";
import { ActionNames, sendAction } from "@/utils/tracking";
import styles from "./index.module.scss";

export default function UserOrderDetails() {
  const [orderStatusData] = useOrderStatusData();
  const [paymentStatus, setPaymentStatus] = useState("");
  const [price, setPrice] = useState("");
  const { params } = useRouter();

  useEffect(() => {
    if (orderStatusData?.orderStatus) {
      setPaymentStatus(orderStatusData?.orderStatus.status || "");
      setPrice(orderStatusData?.orderStatus.price || "");
    }
  }, [orderStatusData]);

  return (
    <Layout>
      <View className={styles.wrapper}>
        <UserSubPageBase title={" "} scrollable={false}>
          <View className={styles.status}>
            <Title className={styles.title}>{paymentStatus}</Title>
            <PriceView price={price} type="heading-n" zeroAsNumber />
          </View>
        </UserSubPageBase>
        <C13BottomDock
          onPrimaryClick={{
            cb: () => {

              if (
                params.type &&
                Number(params.type) === ApiItemTypeMapping.ShopItem
              ) {
                requestSubscribeMessage([
                  notifications.order.delivered,
                  notifications.order.revceived,
                ]);
              } else if (
                params.type &&
                Number(params.type) !== ApiItemTypeMapping.ShopItem
              ) {

                requestSubscribeMessage([
                  notifications.event.redeem,
                  notifications.event.cancel,
                  notifications.event.start,
                ]);
              }


              setTimeout(() => {
                if (orderStatusData && orderStatusData.orderNo!.length > 1) {
                  redirectTo({ url: `/pages/user-order-listing/index` });
                } else {
                  redirectTo({
                    url: `/pages/user-order-details/index?orderNo=${orderStatusData?.orderNo[0]}`,
                  });
                }
              }, 1000);

              sendAction(ActionNames.OrderViewFormClick, {});
            },
            text: "查看订单",
          }}
          onSecondaryClick={{
            cb: () => {
              sendAction(ActionNames.OrderBackHomeClick, {});
              switchTab(`pages/home/index`);
            },
            text: "返回",
          }}
        />
      </View>
    </Layout>
  );
}
